package main

import (
	"fmt"
	"net/http"

	"github.com/gin-gonic/gin"
)

func main() {
	fmt.Printf("abd")
	fmt.Print("test")
	r := gin.Default()
	r.GET("/ping", func(c *gin.Context) {
		c.JSON(200, gin.H{
			"message": "pong"})
	})
	r.Use(middle1(), middle2())
	r.GET("/user/:name", middle1(), func(c *gin.Context) {
		firstname := c.DefaultQuery("firstname", "Guest")
		lastname := c.Query("lastname")
		fmt.Printf("firstname=%s, lastname=%s\n", firstname, lastname)
		c.String(http.StatusOK, "hello %s %s", firstname, lastname)
	})
	r.Run(":9090")
}

func middle1() gin.HandlerFunc {
	return func(c *gin.Context) {
		fmt.Println("middle1  before")
		c.Abort()
		fmt.Println("middle1 after")
	}
}

func middle2() gin.HandlerFunc {
	return func(c *gin.Context) {
		fmt.Println("middle2 before")
		c.Next()
		fmt.Println("middle2 after")
	}
}
